package net.zigin.nhi.could.waste.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.zigin.commons.util.AssertUtil;
import net.zigin.commons.util.ReflectUtil;
import net.zigin.nhi.could.waste.annotation.LoginWhitePathAnnotation;
import net.zigin.nhi.could.waste.dto.SysUserStaffDto;
import net.zigin.nhi.could.waste.model.SysUserHospital;
import net.zigin.nhi.could.waste.service.SysUserHospitalService;

import net.zigin.nhi.could.waste.vo.JsonForResult;
import net.zigin.nhi.could.waste.vo.SysUserStaffVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * SysUserStaff控制器类
 *
 * @author mgw1986
 * @version V1.0
 * @ClassName: SysUserStaffController
 * @date 2021-9-7 19:09:00
 */
@Api(tags = "员工管理")
@RestController
@RequestMapping("/sysUserStaff")
public class SysUserStaffController extends BaseController {
    @Autowired
    private SysUserHospitalService sysUserHospitalService;

    /**
     * 保存或编辑员工
     *
     * @param sysUserStaffDto 员工信息
     * @return 员工ID
     */
    @PostMapping("/save")
    @ApiOperation("保存或编辑员工")
    @ApiImplicitParam(name = "sysUserStaffDto", value = "员工信息", required = true, dataType = "SysUserStaffDto")
    public JsonForResult save(@RequestBody SysUserStaffDto sysUserStaffDto) {
        String id = sysUserHospitalService.saveOrUpdate(sysUserStaffDto);
        return JsonForResult.builder().data(id).success(true).build();
    }

    /**
     * 员工详情
     *
     * @param id 员工id
     * @return 员工详情
     */
    @GetMapping("/getById/{id}")
    @ApiOperation("员工详情")
    @ApiImplicitParam(name = "id", value = "员工id", required = true, dataType = "String")
    public JsonForResult<SysUserStaffVo> getById(@PathVariable String id) {
        AssertUtil.isNull(id, "员工id不能为空");
        SysUserHospital userHospital = sysUserHospitalService.selectByPrimaryKey(id);
        SysUserStaffVo userStaffVo = ReflectUtil.copyProperties(userHospital, SysUserStaffVo.class);
        return JsonForResult.<SysUserStaffVo>builder().data(userStaffVo).success(true).build();
    }

    /**
     * 员工列表
     *
     * @param sysUserStaffDto 员工信息
     * @return 员工列表
     */
    @PostMapping("/getList")
    @ApiOperation("员工列表")
    @ApiImplicitParam(name = "sysUserStaffDto", value = "员工信息", required = true, dataType = "SysUserStaffDto")
    public JsonForResult<List<SysUserStaffVo>> getList(@RequestBody SysUserStaffDto sysUserStaffDto) {
        List<SysUserStaffVo> userStaffVos = sysUserHospitalService.getList(sysUserStaffDto);
        Integer count = sysUserHospitalService.getCount(sysUserStaffDto);
        return JsonForResult.<List<SysUserStaffVo>>builder()
                .data(userStaffVos).total(count)
                .success(true).build();
    }

    /**
     * 删除员工
     *
     * @param id 员工id
     * @return
     */
    @DeleteMapping("/remove/{id}")
    @ApiOperation("删除员工")
    @ApiImplicitParam(name = "id", value = "员工id", required = true, dataType = "String")
    public JsonForResult remove(@PathVariable String id) {
        AssertUtil.isNull(id, "员工id不能为空");
        sysUserHospitalService.remove(id);
        return JsonForResult.builder().success(true).build();
    }

    /**
     * 下载二维码
     *
     * @param id       员工ID
     * @param response
     * @return
     */
    @LoginWhitePathAnnotation
    @GetMapping("/generateQrCode/{id}")
    @ApiOperation("下载二维码")
    @ApiImplicitParam(name = "id", value = "员工id", required = true, dataType = "String")
    public JsonForResult generateQrCode(@PathVariable String id, HttpServletResponse response) {
        sysUserHospitalService.generateQrCode(id, response);
        return JsonForResult.builder().success(true).build();
    }

    @ApiOperation("导出人员列表")
    @ApiImplicitParam(name = "sysUserStaffDto", value = "人员信息", required = true, dataType = "SysUserStaffDto")
    @PostMapping("/exportList")
    public void exportList(@RequestBody SysUserStaffDto sysUserStaffDto, HttpServletResponse response) {
        sysUserHospitalService.exportList(sysUserStaffDto, response);
    }

    @ApiOperation("导入人员列表")
    //@ApiImplicitParam(name = "file", value = "文件", required = true, dataType = "MultipartFile")
    @PostMapping("/importList")
    public JsonForResult testImport(@ApiParam(name = "file",value = "file", required = true) MultipartFile file) {
        sysUserHospitalService.importList(file);
        return JsonForResult.builder().success(true).build();
    }

    @ApiOperation("下载模板")
    @GetMapping("/downloadTemplate")
    public void downloadTemplate(HttpServletResponse response) {
        sysUserHospitalService.downloadTemplate(response);
    }

    @PostMapping("/generateQrCode")
    @ApiOperation("批量下载二维码")
    @ApiImplicitParam(name = "ids", value = "员工id列表", required = true, allowMultiple = true, dataType = "String")
    public void generateQrCode(@RequestBody List<String> ids, HttpServletResponse response) {
        sysUserHospitalService.generateQrCode(ids, response);
    }

}
